CS211 DATA STRUCTURES AND ALGORITHMS II
2ND SCIENCE & HIGHER DIPLOMA IN INFORMATION TECHNOLOGY
LABORATORY EXAM 2
SAMPLE QUESTIONS
public class TreeNode {
private Object item;
private Object weight;
private TreeNode leftChild;
private TreeNode rightChild;
public TreeNode(Object newItem) {
// Initializes tree node with item and no children.
item = newItem;
leftChild = null; // reference has no object
rightChild = null;
} // end constructor
public BinaryTreeBasis(Object rootItem) {
root = new TreeNode(rootItem, null, null);
} // end constructor
protected TreeNode insertItem(TreeNode tNode, KeyedItem newItem,
Float weight) {
if (tNode == null) {
// comments
//
} // end if
SYMBOL |
E |
O |
I |
T |
B |
A |
PROBABILITY |
0.074 |
0.092 |
0.08 |
0.120 |
0.101 |
0.071 |
public class Path {
final static int MAXPATHLENGTH = 20;
int PathComponents[] = new int[MAXPATHLENGTH];
int PathLength;
public void Path() {
for(int i=0;i<MAXPATHLENGTH;i++)
PathComponents[i] =0;
PathLength=0;
}
public void AddToPath(int direction) {
if(PathLength < MAXPATHLENGTH)
{
}
else
System.out.println("Error maximum pathlength reached\n");
}
public void RemoveFromPath() {
if (PathLength > 0)
}
public void PrintPath() {
for(int i=0;i<PathLength;i++)
System.out.print(PathComponents[i]);
System.out.println();
}
}
public void LeafNodeHelper(TreeNode tNode, Path code) {
if(tNode != null)
{
// comments
//
//
if((tNode.getLeft()==null) && (tNode.getRight()==null))
{
//comments
//
KeyedItem nodeItem = (KeyedItem)tNode.getItem();
//
//
//
System.out.print(nodeItem.getKey());
code.PrintPath();
}
else
{
// comments
//
//
//
code.AddToPath(0);
LeafNodeHelper(tNode.getLeft(),code);
code.RemoveFromPath();
// comments
//
//
//
code.AddToPath(1);
LeafNodeHelper(tNode.getRight(),code);
code.RemoveFromPath();
}
}
}